package com.yy.wga.mp.module.order.manager;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.yy.wga.mp.module.income.service.IncomeOrderGoodsService;
import com.yy.wga.mp.module.order.entity.pojo.OrderGoodsDO;
import com.yy.wga.mp.module.order.service.OrderGoodsService;
import com.yy.wga.mp.module.income.service.IncomeOrderGoodsService;
import com.yy.wga.mp.module.order.entity.pojo.OrderGoodsDO;
import com.yy.wga.mp.module.order.service.OrderGoodsService;
import com.yy.wga.mp.module.income.service.IncomeOrderGoodsService;
import com.yy.wga.mp.module.merchant.entity.pojo.MerchantInfoDO;
import com.yy.wga.mp.module.merchant.service.MerchantInfoService;
import com.yy.wga.mp.module.order.entity.pojo.OrderGoodsDO;
import com.yy.wga.mp.module.order.service.OrderGoodsService;
import com.yy.wga.mp.module.income.service.IncomeOrderGoodsService;
import com.yy.wga.mp.module.order.entity.pojo.OrderGoodsDO;
import com.yy.wga.mp.module.order.service.OrderGoodsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author: gc
 * @Date: 2024/10/30
 * @Description:
 **/
@Slf4j
@Component
@RequiredArgsConstructor
public class OrderSettleManager {
    private final OrderGoodsService orderGoodsService;
    private final MerchantInfoService merchantInfoService;
    private final IncomeOrderGoodsService incomeOrderGoodsService;

    public static void main(String[] args) {
        System.out.println(DateUtil.beginOfMonth(new Date()).toJdkDate());
    }
    /**
     * 结算
     */
    @Transactional
    public void orderSettle() {
        List<MerchantInfoDO> merchantInfoList = merchantInfoService.list();
        if (CollectionUtils.isEmpty(merchantInfoList)) {
            return;
        }
        Date settleDate = DateUtil.beginOfMonth(new Date()).toJdkDate();
        for (MerchantInfoDO merchantInfo : merchantInfoList) {
            Long id = 0L;
            while (true) {
                PageHelper.startPage(1, 10, false);
                List<OrderGoodsDO> orderGoodsList = orderGoodsService.list(new LambdaQueryWrapper<OrderGoodsDO>()
                        .lt(OrderGoodsDO::getSuccessTime, settleDate)
                        .eq(OrderGoodsDO::getAppKey, merchantInfo.getAppKey())
                        .eq(OrderGoodsDO::getStatus, 6)
                        .ge(OrderGoodsDO::getId, id));
                if (orderGoodsList == null || orderGoodsList.isEmpty()) {
                    break;
                }
                for (OrderGoodsDO orderGoods : orderGoodsList) {
                    OrderGoodsDO update = new OrderGoodsDO();
                    update.setId(orderGoods.getId());
                    update.setStatus(7);
                    update.setSettleTime(new Date());
                    orderGoodsService.updateById(update);

                    // 结算收益
                    incomeOrderGoodsService.orderSettle(orderGoods.getAppKey(), orderGoods.getOrderNo());
                }

                if (orderGoodsList.size() < 10) {
                    break;
                }
                id = orderGoodsList.get(orderGoodsList.size() - 1).getId();
            }
        }
    }
}
